package frame;

import dao.CourseDao;
import dao.ScoreDao;
import dao.StudentDao;
import model.Score;
import model.Student;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.Vector;
//处理与学生成绩相关的操作，例如添加、修改和删除成绩记录，以及查看特定学生的成绩记录
public class StudentScendpanel extends CommonPanel{

    private String ssno;
    private Box center = Box.createHorizontalBox();
    //列的集合
    private Vector columns;

    //学号
    JTextField sno;
    //学生姓名
    JTextField sname;
    //课程号
    JTextField cno;
    //课程名
    JTextField cname;
    //成绩id
    JTextField id;

    //学生
    String  sid;

    //课程ID
    JTextField cid;

    //成绩
    JTextField score;

    JButton clearButton;

    //删除的按钮
    JButton deleButton;

    JButton saveButton;


    private final static String DEFAULT_File_Path = "upload/no_pic.gif";

    private void initColumns() {
        this.columns = new Vector();
        this.columns.add("id");
        this.columns.add("学号");
        this.columns.add("姓名");
        this.columns.add("课程编号");
        this.columns.add("课程");
        this.columns.add("成绩");

    }


    public StudentScendpanel(String ssno) {
        sid = ssno;
        setViewDatas();
        initColumns();

        //设置列表
        DefaultTableModel model = new DefaultTableModel(getDatas(), this.columns);
        JTable table = new CommonJTable(model);
        setJTable(table);

        JScrollPane upPane = new JScrollPane(table);
        upPane.setPreferredSize(new Dimension(1000, 350));

        //设置添加, 修改的界面
        JPanel downPane = new JPanel();
        downPane.setLayout(new BoxLayout(downPane, BoxLayout.X_AXIS));

        Box downBox1 = new Box(BoxLayout.X_AXIS);


        Box downRightBox = new Box(BoxLayout.Y_AXIS);

        //downRightBox.add(p);

//       downPane.add(downLeftBox);
        downPane.add(downRightBox);


        JPanel queryPanel = new JPanel();
        Box queryBox = new Box(BoxLayout.X_AXIS);


        queryBox.add(Box.createHorizontalStrut(30));

        queryPanel.add(queryBox);
        this.add(queryPanel);

        //列表为添加界面
        JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT, upPane, downPane);
        split.setDividerSize(5);
        this.add(split);
        //添加监听器
        //initListeners();
    }

    private void initListeners() {

        clearButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                clear();
            }
        });
    }


        //清空表单, 刷新列表
           public void clear() {
        refreshTable();
        this.id.setText("");
        this.sno.setText("");
        this.sname.setText("");
        this.cno.setText("");
        this.cname.setText("");
        this.score.setText("");

        }

    //保存
    private void save()  {
        StudentDao studentDao = new StudentDao();
        String sno = this.sno.getText();
        String sname = this.sname.getText();
        if (!studentDao.getDataBySno(sno,sname)) {
            showWarn("学号或名字有误");
            return;
        }
//		String sid = student.getId();
        CourseDao courseDao = new CourseDao();
        String cno = this.cno.getText();
        String cname = this.cname.getText();
        if (!courseDao.getDataByCno(cno,cname)) {
            showWarn("课程号或课程名有误");
            return;
        }
        ScoreDao scoreDao = new ScoreDao();
        if(scoreDao.getDataByOne(sno,cname)){
            showWarn("该学生课程已经录入");
            return;
        }
//		String cid = course.getId();
        String tsno = this.sno.getText();
        String tcno = this.cno.getText();
        String tsname = this.sname.getText();
        String tcname = this.cname.getText();
        float tscore = Float.parseFloat(this.score.getText());
        String id = this.id.getText();
        Score score1 = new Score();
        score1.setSno(tsno);
        score1.setSname(tsname);
        score1.setCno(tcno);
        score1.setCname(tcname);
        score1.setScore(tscore
        );

        score1.setId(id);
        ScoreDao score1Dao = new ScoreDao();
        try {
            if(id==null || id.equals("")) {
                score1Dao.insertData(score1);
                JOptionPane.showMessageDialog(center, "保存成功", "成功", JOptionPane.INFORMATION_MESSAGE);
            }else {
                score1Dao.updateData(score1);
                JOptionPane.showMessageDialog(center, "更改成功", "成功", JOptionPane.INFORMATION_MESSAGE);
            }
            setViewDatas();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.print("修改数据失败");
            JOptionPane.showMessageDialog(center, "修改失败", "error", JOptionPane.ERROR_MESSAGE);
        }

        setViewDatas();
        clear();
    }


    //验证输入
    private boolean validateScore() {
        String score = this.score.getText();
        try {
            Integer p = Integer.parseInt(score);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    //查看成绩
    private void view() {
        try {
            String id = getSelectId(getJTable());
            ScoreDao scoreDao = new ScoreDao();
            Score sc = scoreDao.getData(id);
            this.id.setText(sc.getId());
            this.sno.setText(sc.getSno());
            this.sname.setText(sc.getSname());
            this.cno.setText(sc.getCno());
            this.cname.setText(sc.getCname());
            this.score.setText(String.valueOf(sc.getScore()));
        }catch(Exception e) {

        }

    }

    public void setViewDatas() {
        try {
            ScoreDao scoreDao = new ScoreDao();
            //查找对应的数据
            Vector<Score> scores = (Vector<Score>)scoreDao.getDatas(sid);
            //转换显示格式
            Vector<Vector> datas =  changeDatas(scores);
            //调用父类方法设置结果集合
            setDatas(datas);
            refreshTable();
        }catch(Exception e) {

        }

    }

    //将数据转换成视图表格的格式
    private Vector<Vector> changeDatas(Vector<Score> datas) {
        Vector<Vector> view = new Vector<Vector>();
        for (Score score : datas) {
            Vector v = new Vector();
            v.add(score.getId());
            v.add(score.getSno());
            v.add(score.getSname());
            v.add(score.getCno());
            v.add(score.getCname());
            v.add(score.getScore());
            view.add(v);
        }
        return view;
    }

    @Override
    public Vector<String> getColumns() {
        return this.columns;
    }

    @Override
    public void setTableFace() {
        //隐藏id列
        getJTable().getColumn("id").setMinWidth(-1);
        getJTable().getColumn("id").setMaxWidth(-1);
        getJTable().setRowHeight(30);
    }

    public void dele() {
        String id = this.id.getText();
        if(id==null || id.equals("")) {
            System.out.print("请选择删除的数据");
        }else {
            ScoreDao scoreDao = new ScoreDao();
            try {
                scoreDao.deleData(id);
                JOptionPane.showMessageDialog(center, "删除成功", "成功", JOptionPane.INFORMATION_MESSAGE);
                setViewDatas();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                JOptionPane.showMessageDialog(center, "删除失败", "error", JOptionPane.ERROR_MESSAGE);
                System.out.print("修改删除失败");
            }
        }
    }

}
